#pragma once

#include <iostream>
#include <cassert>

using namespace std;

namespace key
{
	template<class K>
	struct BSTreeNode
	{
		typedef BSTreeNode<K> Node;

		Node* _left;
		Node* _right;
		K _key;

		BSTreeNode(const K& key)
			: _left(nullptr)
			, _right(nullptr)
			, _key(key)
		{}
	};

	template<class K>
	class BSTree
	{
	public:
		typedef BSTreeNode<K> Node;

		BSTree() = default;

		BSTree(const BSTree<K>& tree)
		{
			_root = copy(tree._root);
		}

		BSTree<K>& operator= (BSTree<K> tree)
		{
			swap(_root, tree._root);

			return *this;
		}

		~BSTree()
		{
			destory(_root);
		}

		void inorder()
		{
			_inorder(_root);
			cout << endl;
		}

		bool find(const K& key)
		{
			Node* cur = _root;
			while (cur)
			{
				if (cur->_key > key)
					cur = cur->_left;
				else if (cur->_key < key)
					cur = cur->_right;
				else
					return true;
			}

			return false;
		}

		bool insert(const K& key)
		{
			Node* newNode = new Node(key);

			if (_root == nullptr)
			{
				_root = newNode;
				return true;
			}

			Node* parent = _root;
			Node* cur = _root;
			while (cur)
			{
				parent = cur;
				if (cur->_key > key)
					cur = cur->_left;
				else if (cur->_key < key)
					cur = cur->_right;
				else
					return false;
			}

			if (key > parent->_key)
				parent->_right = newNode;
			else
				parent->_left = newNode;

			return true;
		}

		bool erase(const K& key)
		{
			assert(_root);

			Node* parent = _root;
			Node* cur = _root;
			while (cur)
			{
				if (cur->_key > key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else if (cur->_key < key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else
				{
					if (cur->_left == nullptr)
					{
						if (cur == parent)
							_root = cur->_right;
						else if (cur == parent->_left)
							parent->_left = cur->_right;
						else
							parent->_right = cur->_right;

						delete cur;
						return true;
					}
					else if (cur->_right == nullptr)
					{
						if (cur == parent)
							_root = cur->_left;
						else if (cur == parent->_left)
							parent->_left = cur->_left;
						else
							parent->_right = cur->_left;

						delete cur;
						return true;
					}
					else
					{
						Node* rightMin = cur->_right;
						Node* rightParent = cur;
						while (rightMin->_left)
						{
							rightParent = rightMin;
							rightMin = rightMin->_left;
						}

						cur->_key = rightMin->_key;

						if (rightMin == rightParent->_right)
							rightParent->_right = rightMin->_right;
						else
							rightParent->_left = rightMin->_right;

						delete rightMin;
						return true;
					}
				}
			}

			return false;
		}

		bool findR(const K& key)
		{
			return _findR(_root, key);
		}

		bool insertR(const K& key)
		{
			return _insertR(_root, key);
		}

		bool eraseR(const K& key)
		{
			return _eraseR(_root, key);
		}

		void inOrder(BSTreeNode<K>* cur, BSTreeNode<K>* prev)
		{
			if (cur == nullptr)
				return;

			inOrder(cur->_left, prev);

			cur->_left = prev;
			if (prev)
				prev->_right = cur;
			prev = cur;

			inOrder(cur->_right, prev);
		}

		BSTreeNode<K>* Convert(BSTreeNode<K>* root)
	{
			if (root == nullptr)
				return nullptr;

			BSTreeNode<K>* prev = nullptr;
			inOrder(root, prev);

			BSTreeNode<K>* head = root;
			BSTreeNode<K>* tail = root;
			while (head->_left)
				head = head->_left;

			return head;
		}

		Node* _root = nullptr;
private:
		Node* copy(Node* root)
		{
			if (root == nullptr)
				return nullptr;

			Node* newNode = new Node(root->_key);
			newNode->_left = copy(root->_left);
			newNode->_right = copy(root->_right);

			return newNode;
		}

		void destory(Node* root)
		{
			if (root == nullptr)
				return;

			destory(root->_left);
			destory(root->_right);
			delete root;
		}

		void _inorder(Node* root)
		{
			if (root == nullptr)
				return;

			_inorder(root->_left);
			cout << root->_key << ' ';
			_inorder(root->_right);
		}

		bool _findR(Node* root, const K& key)
		{
			if (root == nullptr)
				return false;

			//if (root->_key > key)
			//	return _findR(root->_left, key);
			//else if (root->_key < key)
			//	return _findR(root->_right, key);
			//else
			//	return true;

			return root->_key == key || _findR(root->_left, key) || _findR(root->_right, key);
		}

		bool _insertR(Node*& root, const K& key)
		{
			if (root == nullptr)
			{
				Node* newNode = new Node(key);
				root = newNode;

				return true;
			}

			if (root->_key > key)
				return _insertR(root->_left, key);
			else if (root->_key < key)
				return _insertR(root->_right, key);
			else
				return false;
		}

		bool _eraseR(Node*& root, const K& key)
		{
			if (root == nullptr)
				return false;

			if (root->_key > key)
				return _eraseR(root->_left, key);
			else if (root->_key < key)
				return _eraseR(root->_right, key);
			else
			{
				Node* del = root;

				if (root->_left == nullptr)
					root = root->_right;
				else if (root->_right == nullptr)
					root = root->_left;
				else
				{
					Node* rightMin = root->_right;
					while (rightMin->_left)
						rightMin = rightMin->_left;

					swap(root->_key, rightMin->_key);

					return _eraseR(root->_right, key);

					/*Node* rightMin = root->_right;
					Node* rightParent = root;
					while (rightMin->_left)
					{
						rightParent = rightMin;
						rightMin = rightMin->_left;
					}

					root->_key = rightMin->_key;

					if (rightMin == rightParent->_right)
						rightParent->_right = rightMin->_right;
					else
						rightParent->_left = rightMin->_right;

					del = rightMin;*/
				}

				delete del;
				return true;
			}
		}
	};
}

namespace key_value
{
	template<class K, class V>
	struct BSTreeNode
	{
		typedef BSTreeNode<K, V> Node;

		Node* _left;
		Node* _right;
		K _key;
		V _value;

		BSTreeNode(const K& key, const V& value)
			: _left(nullptr)
			, _right(nullptr)
			, _key(key)
			, _value(value)
		{}
	};

	template<class K, class V>
	class BSTree
	{
	public:
		typedef BSTreeNode<K, V> Node;

		BSTree() = default;

		BSTree(const BSTree<K, V>& tree)
		{
			_root = copy(tree._root);
		}

		BSTree<K, V>& operator= (BSTree<K, V> tree)
		{
			swap(_root, tree._root);

			return *this;
		}

		~BSTree()
		{
			destory(_root);
		}

		void inorder()
		{
			_inorder(_root);
			cout << endl;
		}

		Node* find(const K& key)
		{
			Node* cur = _root;
			while (cur)
			{
				if (cur->_key > key)
					cur = cur->_left;
				else if (cur->_key < key)
					cur = cur->_right;
				else
					return cur;
			}

			return nullptr;
		}

		bool insert(const K& key, const V& value)
		{
			Node* newNode = new Node(key, value);

			if (_root == nullptr)
			{
				_root = newNode;
				return true;
			}

			Node* parent = _root;
			Node* cur = _root;
			while (cur)
			{
				parent = cur;
				if (cur->_key > key)
					cur = cur->_left;
				else if (cur->_key < key)
					cur = cur->_right;
				else
					return false;
			}

			if (key > parent->_key)
				parent->_right = newNode;
			else
				parent->_left = newNode;

			return true;
		}

		bool erase(const K& key)
		{
			assert(_root);

			Node* parent = _root;
			Node* cur = _root;
			while (cur)
			{
				if (cur->_key > key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else if (cur->_key < key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else
				{
					if (cur->_left == nullptr)
					{
						if (cur == parent)
							_root = cur->_right;
						else if (cur == parent->_left)
							parent->_left = cur->_right;
						else
							parent->_right = cur->_right;

						delete cur;
						return true;
					}
					else if (cur->_right == nullptr)
					{
						if (cur == parent)
							_root = cur->_left;
						else if (cur == parent->_left)
							parent->_left = cur->_left;
						else
							parent->_right = cur->_left;

						delete cur;
						return true;
					}
					else
					{
						Node* rightMin = cur->_right;
						Node* rightParent = cur;
						while (rightMin->_left)
						{
							rightParent = rightMin;
							rightMin = rightMin->_left;
						}

						cur->_key = rightMin->_key;

						if (rightMin == rightParent->_right)
							rightParent->_right = rightMin->_right;
						else
							rightParent->_left = rightMin->_right;

						delete rightMin;
						return true;
					}
				}
			}

			return false;
		}

		Node* findR(const K& key)
		{
			return _findR(_root, key);
		}

	private:
		Node* _root = nullptr;

		Node* _findR(Node* root, const K& key)
		{
			if (root == nullptr)
				return nullptr;

			Node* ret = nullptr;
			if (root->_key > key)
				ret = _findR(root->_left, key);
			else if (ret == nullptr && root->_key < key)
				ret = _findR(root->_right, key);
			else
				ret = root;

			return ret;
		}

		Node* copy(Node* root)
		{
			if (root == nullptr)
				return nullptr;

			Node* newNode = new Node(root->_key);
			newNode->_left = copy(root->_left);
			newNode->_right = copy(root->_right);

			return newNode;
		}

		void destory(Node* root)
		{
			if (root == nullptr)
				return;

			destory(root->_left);
			destory(root->_right);
			delete root;
		}

		void _inorder(Node* root)
		{
			if (root == nullptr)
				return;

			_inorder(root->_left);
			cout << root->_key << ' ';
			_inorder(root->_right);
		}
	};
}



void Test()
{
	int element[] = { 10, 6, 14, 4, 8, 12, 16 };
	key_value::BSTree<int, int> tree;

	for (auto& i : element)
		tree.insert(i,i);

	tree.inorder();
	for (auto& i : element)
	{
		cout << tree.findR(i)->_value << ' ';
	}
	
}


//void Test()
//{
//	int element[] = { 5, 1, 8, 0, 3, 6, 9, 2, 4, 7 };
//	//int element[] = { 5, 8, 6, 9, 7 };
//	//int element[] = { 5, 1, 0, 3, 2, 4};
//	BSTree<int> tree;
//	
//	for (auto& i : element)
//		tree.insertR(i);
//
//	for (auto& i : element)
//		cout << tree.find(i) << ' ';
//	cout << endl;
//
//	//tree.erase(5);
//
//	//for (auto& i : element)
//	//{
//	//	tree.erase(i);
//	//	tree.inorder();
//	//}
//
//	tree.inorder();
//}
//void Test()
//{
//	int element[] = { 5, 1, 8, 0, 3, 6, 9, 2, 4, 7 };
//	BSTree<int> tree;
//
//	for (auto& i : element)
//		tree.insertR(i);
//
//	for (auto& i : element)
//	{
//		tree.erase(i);
//		tree.inorder();
//	}
//}
//void Test()
//{
//	int element[] = { 5, 1, 8, 0, 3, 6, 9, 2, 4, 7 };
//	BSTree<int> tree;
//
//	for (auto& i : element)
//		tree.insertR(i);
//
//	BSTree<int> tree1(tree);
//
//	tree1.inorder();
//}